<template>
  <el-dialog :title="this.dataForm.id?'课程类目修改':'课程类目新增'" :close-on-click-modal="false" :visible.sync="messageShow" class="view">
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
      :rules="dataRule"
    >
      <el-form-item label="课程类目名称" prop="bookName">
        <el-input v-model="dataForm.bookName" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确定</el-button>
      <el-button @click="messageShow = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        dataForm: {},
        steward: [{}],
        messageShow: false,
        dataRule: {
          bookName: [
            {required: true, message: '课程类目名称不能为空', trigger: 'blur'}
          ],
        }
      }
    },
    methods: {
      initData(obj) {
        this.messageShow = true
        this.$nextTick(()=> {
          if (obj) {
            this.dataForm = {...obj}
          } else {
            this.$refs['dataForm'].clearValidate()
            this.dataForm = {}
          }
        })
      },
      dataFormSubmit() {
        const data = {
          ...this.dataForm,
          bookType: 4
        }
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/bookinfo/${data.id ? 'updateCourseCategory' : 'saveCourseCategory'}`),
              method: "post",
              data
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.messageShow = false
                this.$emit('refresh')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  };
</script>
<style scoped>
  .otherContent {
    padding: 0 60px;
  }

  .el-row {
    display: flex;
    align-items: center;
    height: 45px;
    font-size: 16px;
  }
</style>
